#include "me_ywy_fmod_fmodtest_VoiceFix.h"
#include "inc/fmod.hpp"
#include "android/log.h"
#include "unistd.h"

#define TAG "YWY_JNI"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO , TAG , __VA_ARGS__);
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , TAG , __VA_ARGS__);

using namespace FMOD;

JNIEXPORT void JNICALL Java_me_ywy_fmod_fmodtest_VoiceFix_fix
        (JNIEnv *env, jclass javaClazz, jstring path_jni, jint mode) {

    const char *path = env->GetStringUTFChars(path_jni, NULL);
    LOGI("path = %s" , path);
    System *system;
    Sound *sound;
    DSP *dsp;
    float speed = 1;
    Channel *channel = 0;
    bool playing = true;
    FMOD_RESULT result;
    unsigned int version;


    try {

        result = System_Create(&system);

        result = system->getVersion(&version);

        result = system->init(32, FMOD_INIT_NORMAL, NULL);

        result = system->createSound(path, FMOD_DEFAULT, 0, &sound);

        switch (mode) {
            case me_ywy_fmod_fmodtest_VoiceFix_MODE_NORMAL:
                LOGI("正常声音");
                result = system->playSound(sound, 0, false, &channel);
                break;
            case me_ywy_fmod_fmodtest_VoiceFix_MODE_LUOLI:
                LOGI("萝莉声音");
                system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT , &dsp);
                dsp->setParameterFloat(FMOD_DSP_TYPE_PITCHSHIFT , 3.5);
                result = system->playSound(sound, 0, false, &channel);
                channel->addDSP(0,dsp);
                break;
            case me_ywy_fmod_fmodtest_VoiceFix_MODE_DASHU:
                LOGI("大叔声音");
                system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT , &dsp);
                dsp->setParameterFloat(FMOD_DSP_TYPE_PITCHSHIFT , 0.8);
                result = system->playSound(sound, 0, false, &channel);
                channel->addDSP(0,dsp);
                break;
            case me_ywy_fmod_fmodtest_VoiceFix_MODE_JINGSONG:
                LOGI("惊悚声音");
                system->createDSPByType(FMOD_DSP_TYPE_TREMOLO , &dsp);
                dsp->setParameterFloat(FMOD_DSP_TREMOLO_SKEW , 0.5);
                result = system->playSound(sound, 0, false, &channel);
                channel->addDSP(0,dsp);
                break;
            case me_ywy_fmod_fmodtest_VoiceFix_MODE_GAOGUAI:
                LOGI("搞怪声音");
                system->createDSPByType(FMOD_DSP_TYPE_NORMALIZE , &dsp);
                result = system->playSound(sound, 0, false, &channel);
                channel->addDSP(0,dsp);
                channel->getFrequency(&speed);
                speed = speed * 2.6;
                channel->setFrequency(speed);
                break;
            case me_ywy_fmod_fmodtest_VoiceFix_MODE_KONGLING:
                LOGI("空灵声音");
                result = system->playSound(sound, 0, false, &channel);
                break;
            default:
                break;
        }


    } catch (...) {
        LOGE("报错");
        goto END;
    }


    result = system->update();

    while(playing){
        channel -> isPlaying(&playing);
        usleep(10000);
    }

    END:
    LOGI("结束");

    env->ReleaseStringUTFChars(path_jni, path);
    result = sound->release();
    result = system->close();
    result = system->release();
    result = dsp->release();
}